package com.lagou.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lagou.dao.UserMapper;
import com.lagou.domain.*;
import com.lagou.service.UserMapperService;
import com.lagou.utils.Md5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author 李树飞
 */
@Service
public class UserMapperServiceImpl implements UserMapperService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 用户分页 & 条件查询
     * @param userVo
     * @return
     */
    @Override
    public PageInfo<User> findAllUserByPage(UserVo userVo) {
        PageHelper.startPage(userVo.getCurrentPage(), userVo.getPageSize());
        List<User> list = userMapper.findAllUserByPage(userVo);
        return new PageInfo<>(list);
    }

    @Override
    public void updateUserStatus(Integer id, String status) {
        User user = new User();
        user.setId(id);
        user.setStatus(status);
        user.setUpdate_time(new Date());
        userMapper.updateUserStatus(user);
    }

    @Override
    public User login(User user) throws Exception {
        //获取到用户信息
        User loginUser = userMapper.login(user);
        if (loginUser != null && Md5.verify(user.getPassword(), Md5.md5key, loginUser.getPassword())) {
            return loginUser;
        } else {
            return null;
        }
    }

    @Override
    public List<Role> findUserRelationRoleById(Integer id) {
        return userMapper.findUserRelationRoleById(id);
    }

    @Override
    public void userContextRole(UserVo userVo) {
        //删除原有的信息
        userMapper.deleteUserContextRole(userVo.getUserId());
        //添加新的信息
        for (Integer roleId : userVo.getRoleIdList()) {
            //封装数据
            User_Role_relation user_role_relation = new User_Role_relation();
            user_role_relation.setUserId(userVo.getUserId());
            user_role_relation.setRoleId(roleId);
            user_role_relation.setCreatedTime(new Date());
            user_role_relation.setUpdatedTime(new Date());
            user_role_relation.setCreatedBy("system");
            user_role_relation.setUpdatedby("system");
            userMapper.userContextRole(user_role_relation);
        }
    }

    /*
    获取用户权限，进行菜单动态展示
     */
    @Override
    public ResponseResult getUserPermissions(Integer id) {
        //获取用户对应的角色信息
        List<Role> roleList = userMapper.findUserRelationRoleById(id);
        //获取角色id，保存到list集合
        List<Integer> roleIds = new ArrayList<>();
        for (Role role : roleList) {
            roleIds.add(role.getId());
        }

        //根据角色id查询父菜单
        List<Menu> parentMenu = userMapper.findParentMenuByRoleId(roleIds);

        //查询父菜单对应的子菜单
        for (Menu menu : parentMenu) {
            List<Menu> subMenu = userMapper.findSubMenuByPid(menu.getId());
            menu.setSubMenuList(subMenu);
        }

        //获取资源信息
        List<Resource> resource = userMapper.findResourceByRoleid(roleIds);

        //封装数据并返回
        Map<String, Object> map = new HashMap<>();
        map.put("menuList", parentMenu);
        map.put("resourceList",resource);
        return new ResponseResult(true,200,"查找成功",map);
    }
}
